package baoxiu.controller;



import baoxiu.entity.Building;
import baoxiu.entity.BulidingDetail;
import baoxiu.service.BuildingDetailService;

import baoxiu.service.BuildingService;
import baoxiu.vo.DropDown;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import common.util.Res;
import common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName BuildingControllee
 * @Author mengjiafeng
 * @Date 2019/7/24 19:48
 **/
@RestController
@RequestMapping("/baoxiu/buildDetail")
public class BuildingDetailController {
    @Autowired
    private BuildingDetailService buildingDetailService;
    @Autowired
    private BuildingService buildingService;
    @GetMapping("/findAll")
    public Result selectAll(BulidingDetail bulidingDetail,Page page){
        return Res.makeOKRsp(buildingDetailService.page(page,new QueryWrapper<BulidingDetail>().like("build_name",bulidingDetail.getBuildName())));
    }
    @GetMapping("/findAllByType")
    public Result selectAllByType(String buildType){
        List<BulidingDetail> detailList = buildingDetailService.list(new QueryWrapper<BulidingDetail>().eq("build_type", buildType));
//                .stream().map(item->{
//                DropDown dropDown = new DropDown();
//                dropDown.setLabel(item.getBuildName());
//                dropDown.setValue(item.getId());
//                return dropDown;
//            }
//        ).collect(Collectors.toList());
        return Res.makeOKRsp(detailList);
    }

    @PostMapping("/add")
    public Result add(BulidingDetail bulidingDetail){
        Building building = buildingService.getById(bulidingDetail.getBuildType());
        bulidingDetail.setTypeName(building.getName());
        buildingDetailService.save(bulidingDetail);
        return Res.makeOKRsp("添加成功");
    }
    @GetMapping("/delete")
    public Result delete(String id){
        buildingDetailService.removeById(id);
        return Res.makeOKRsp("删除成功");

    }
}
